﻿/*using Irony.Parsing;
using NMF.Transformations;
using NMF.Transformations.Simples;
using NMF.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NMF.Languages.AnyText.Transformations
{
    public class Initializer : Transformation
    {
        public object CreateObject(ParseTree input)
        {
            return TransformationEngine.Transform<ParseTreeNode, object, object>
                (input.Root, null, this);
        }

        protected override IEnumerable<GeneralTransformationRule> CreateAllTransformationRules()
        {
            yield return new NonTerminalInitializationRule();
            yield return new TypedNonterminalInitializationRule();
            yield return new DataTypeTerminalRule();
            yield return new IdentifierRule();
            yield return new IdentifierReferenceRule();
            yield return new RuleInitializationRule();
        }

        class NonTerminalInitializationRule : SimpleTransformationRule<ParseTreeNode, object, object>
        {
            public override object CreateOutput(ParseTreeNode input, object inContext, ITransformationContext context)
            {
                return inContext;
            }

            public override void RegisterRequirements()
            {
                CallManyOutputSensitive(Rule<NonTerminalInitializationRule>(), GetChildTerms);
            }

            private IEnumerable<Tuple<ParseTreeNode, object>> GetChildTerms(ParseTreeNode input, object inObject, object outObject)
            {
                var term = input.Term as NonTerminal;
                if (term != null)
                {
                    foreach (var item in input.ChildNodes)
                    {
                        yield return new Tuple<ParseTreeNode, object>(item, outObject);
                    }
                }
            }
        }

        class TypedNonterminalInitializationRule : AbstractTransformationRule<ParseTreeNode, object, object>
        {
            public override void Transform(ParseTreeNode input, object inContext, object outContext, ITransformationContext context)
            {
                var term = input.Term as TypedNonTerminal;
                if (term.Property != null)
                {
                    var prop = term.Property.Property;
                    if (prop != null)
                    {
                        if (prop.IsCollectionProperty && prop is ICollectionProperty)
                        {
                            var collProp = prop as ICollectionProperty;
                            collProp.AddItem(inContext, outContext);
                        }
                        else
                        {
                            prop.SetValue(inContext, outContext);
                        }
                    }
                    else
                    {
                        throw new Exception("Property is missing");
                    }
                }
            }

            public override void RegisterRequirements()
            {
                MarkInstantiatingFor(Rule<NonTerminalInitializationRule>(), (pn, o) => pn.Term is TypedNonTerminal);
            }
        }

        class DataTypeTerminalRule : SimpleTransformationRule<ParseTreeNode, object, object>
        {
            public override object CreateOutput(ParseTreeNode input, object context, ITransformationContext context2)
            {
                var term = input.Term as DataTypeTerminal;
                return term.GetValue(input);
            }

            public override void RegisterRequirements()
            {
                MarkInstantiatingFor(Rule<TypedNonterminalInitializationRule>(), (pn,o) => pn.Term is DataTypeTerminal);
            }
        }

        class IdentifierRule : SimpleTransformationRule<ParseTreeNode, object, object>
        {
            public override object CreateOutput(ParseTreeNode input, object context, ITransformationContext context2)
            {
                var identifier = input.Term as Identifier;
                object dictionary;
                IDictionary<string, object> dict;
                if (!context2.Data.TryGetValue(identifier.Name, out dictionary))
                {
                    dict = new Dictionary<string, object>();
                    context2.Data.Add(identifier.Name, dict);
                }
                else
                {
                    dict = dictionary as IDictionary<string, object>;
                }
                if (dict != null)
                {
                    var name = (string)identifier.GetValue(input);
                    dict.Add(name, context);
                    return name;
                }
                else
                {
                    throw new Exception("Somebody modified the data dictionary...");
                }
            }

            public override void RegisterRequirements()
            {
                MarkInstantiatingFor(Rule<DataTypeTerminalRule>(), (pn,o) => pn.Term is Identifier);
            }
        }

        class IdentifierReferenceRule : SimpleTransformationRule<ParseTreeNode, object, object>
        {
            public override object CreateOutput(ParseTreeNode input, object context, ITransformationContext context2)
            {
                var reference = input.Term as IdentifierReference;
                var identifier = reference.Identifier;
                object dictionary;
                IDictionary<string, object> dict;
                if (!context2.Data.TryGetValue(identifier.Name, out dictionary))
                {
                    dict = new Dictionary<string, object>();
                    context2.Data.Add(identifier.Name, dict);
                }
                else
                {
                    dict = dictionary as IDictionary<string, object>;
                }
                if (dict != null)
                {
                    object output;
                    var referencedItem = (string)input.ChildNodes[0].Token.Value;
                    if (dict.TryGetValue(referencedItem, out output))
                    {
                        return output;
                    }
                    else
                    {
                        if (reference.MustExist)
                        {
                            throw new InvalidOperationException(string.Format("The reference {0} of type {1} could not be found.", referencedItem, identifier.Name));
                        }
                        else
                        {
                            return identifier.IdentifiedClass.CreateInstance();
                        }
                    }
                }
                else
                {
                    throw new Exception("Somebody modified the data dictionary...");
                }
            }

            public override void RegisterRequirements()
            {
                MarkInstantiatingFor(Rule<TypedNonterminalInitializationRule>(), (pn,o) => pn.Term is IdentifierReference);

                OutputDelayLevel = 1;
            }
        }

        class RuleInitializationRule : SimpleTransformationRule<ParseTreeNode, object, object>
        {
            public override object CreateTransformationOutput(ParseTreeNode input, object inContext, ITransformationContext context)
            {
                var rule = input.Term as RuleNonTerminal;
                if (inContext != null && !rule.IsProperty)
                {
                    return inContext;
                }
                else
                {
                    return rule.Class.CreateInstance();
                }
            }

            public override void RegisterRequirements()
            {
                MarkInstantiatingFor(Rule<TypedNonterminalInitializationRule>(), (pn,o) => pn.Term is RuleNonTerminal);
            }
        }


    }
}
*/